<?php
class Get_task_record extends MY_Controller
{	
	function __construct() {
        parent::__construct();
    }
    function index(){
        // print_r($_POST);exit;
    	$data = $this->user_post_data;
    	if(!empty($data)){
            $sql = "SELECT task_record.*,task.name,task_type.image as type_img,task.stars as count_stars,childer.`nick` FROM tiku_task_records task_record
                    LEFT JOIN tiku_tasks task on task.id = task_record.task_id
                    LEFT JOIN tiku_user_childrens childer on childer.id = task_record.child_id 
                    LEFT JOIN tiku_task_types task_type on task_type.id=task.`task_type_id`
                    where task_record.user_id={$data['user_token']['user_id']}";

            $result = $this->db->query($sql)->result_array();
            $data_list = array();
            foreach ($result as $res) {
                $data_one = array(
                    'id' => $res['id'],
                    'img' => $res['type_img'],
                    'ago' => time_ago($res['time']),
                    'name' => $res['name'],
                    'nick' => $res['nick'],
                    'stars' => $res['stars'],
                    'count_stars' => $res['count_stars']
                );
                $data_list[] = $data_one;
            }
            $data_obj = array(
                'count' => count($result),
                'list' => $data_list,
             );
            echo json_encode(array("code" => 1, "data" => $data_obj));   // 
    		 // echo $userid;
    	}else{
            echo json_encode(array("code" => 404, "user" => 'not user'));
        }
    }

    /*function task_add_start(){
        $data = $this->user_post_data;
        if(!empty($data)){
            //获取任务数据，限制最多可以加多少星，防止被刷
            $task_info = get_field_by_where("tasks", "id,stars,name", "id='{$data['task_id']}'");

            //获取任务记录数据，如果没有记录就要添加新记录
            $today = strtotime(date("Y-m-d", time()));
            $where = "child_id='{$data['user_token']['child_id']}' AND time >= $today AND task_id = '{$data['task_id']}'";
            $task_record_info = get_field_by_where("task_records", "*", $where);
            if($task_record_info){
                $update = true;
                $stars = 0;
                $status = $data['stars'];
                $id = $task_record_info['id'];
                if($status==0){
                    $stars = -1;
                    if($task_record_info['stars'] == 0){
                        $update = false;
                    }
                }else if($status==1){
                    $stars = 1;
                    if($task_record_info['stars'] >= $task_info['stars']){
                        $update = false;
                    }
                }
                if($update){
                    $sql = "UPDATE tiku_task_records SET stars = (stars + {$stars}) WHERE id = {$id}";
                    $this->db->query($sql);
                }
            }else{
                $task_recode_data['user_id']    = $data['userinfo']['user_id'];
                $task_recode_data['child_id']   = $data['userinfo']['child_id'];
                $task_recode_data['task_id']    = $data['task_id'];
                $task_recode_data['task_name']  = $task_info['name'];
                $task_recode_data['stars']      = $data['stars'];
                $task_recode_data['time']       = time();
                $task_recode_data['status']     = 1;
                $this->db->insert("task_records", $task_recode_data);
            }
            
           
            echo json_encode(array("code" => 1, "data" => null));   //  
             // echo $userid;
        }else{
            echo json_encode(array("code" => 404, "user" => 'not user'));
        }
    }*/


}